<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="pd.showSearch">
        <el-form :model="pd.queryParams" ref="queryFormRef" :inline="true" label-width="68px">

          <el-form-item label="物品" prop="productId">
            <ProductSearch title="物品查询" @ok="searchProduct"/>
          </el-form-item>

          <el-form-item label="批次号" prop="batchCode">
            <el-input v-model="pd.queryParams.batchCode" placeholder="请输入批次号" clearable style="width: 240px"
                      @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item label="仓库" prop="warehouseId">
            <el-select v-model="pd.queryParams.warehouseId" placeholder="操作类型" clearable style="width: 240px">
              <el-option v-for="dict in wms_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="供应商" prop="supplierId">
            <PartnerSearch title="供应商查询" :params="{isSupplier:'0'}" @ok="searchSupplier"/>
          </el-form-item>
          <el-form-item label="库存有效期" prop="expireDate">
            <el-date-picker clearable v-model="pd.queryParams.expireDate" type="date"
                            value-format="YYYY-MM-DD" placeholder="请选择库存有效期"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>
    <el-card shadow="never">
      <el-table v-loading="pd.loading" :data="pd.stockList">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="ID" align="center" prop="id"/>
        <el-table-column label="物品编码" align="center" prop="productId"/>
        <el-table-column label="单位" align="center" prop="measureUnit"/>
        <el-table-column label="批次号" align="center" prop="batchCode"/>
        <el-table-column label="仓库" align="center" prop="warehouseId"/>
        <el-table-column label="库位" align="center" prop="areaId"/>
        <el-table-column label="供应商" align="center" prop="supplierId"/>
        <el-table-column label="生产厂商" align="center" prop="manufacturerId"/>
        <el-table-column label="在库数量" align="center" prop="quantity"/>
        <el-table-column label="采购单价" align="center" prop="purchasePrice"/>
        <el-table-column label="库存有效期" align="center" prop="expireDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark"/>
      </el-table>

      <pagination v-show="pd.total>0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                  v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>

    </el-card>

  </div>
</template>

<script setup name="Stock" lang="ts">
import {listStock} from '@/api/wms/stock';
import {StockQuery, StockVO} from '@/api/wms/stock/types';
import ProductSearch from '@/views/wms/product/search.vue';
import PartnerSearch from '@/views/hrp/partner/search.vue';
import {PartnerVO} from "@/api/hrp/partner/types";
import {ProductVO} from "@/api/wms/product/types";

const {proxy} = getCurrentInstance() as ComponentInternalInstance;
const {wms_type} = toRefs<any>(proxy?.useDict("wms_type"));

const queryFormRef = ref<ElFormInstance>();

const pd = reactive({
  stockList: <StockVO[]>([]),
  loading: false,
  showSearch: true,
  ids: <Array<string | number>>([]),
  single: true,
  multiple: true,
  total: 0,
  queryParams: <StockQuery>{
    pageNum: 1,
    pageSize: 10,
    productId: undefined,
    batchCode: undefined,
    warehouseId: undefined,
    areaId: undefined,
    expireDate: undefined,
    supplierId: undefined,
    params: {}
  }
});

/** 查询库存数量列表 */
async function getList() {
  pd.loading = true;
  const res = await listStock(pd.queryParams);
  pd.stockList = res.rows;
  pd.total = res.total;
  pd.loading = false;
}

function searchProduct(vo: ProductVO[]) {
  vo.forEach((v, i, a) => {
    pd.queryParams.productId = v.id;
  });
}

function searchSupplier(vo: PartnerVO[]) {
  vo.forEach((v, i, a) => {
    pd.queryParams.supplierId = v.id;
  });
}

/** 搜索按钮操作 */
const handleQuery = () => {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('wms/stock/export', {
    ...pd.queryParams
  }, `stock_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
